import util from "../../../../utils/util";
const app = getApp();
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
var allInfo = [];
var leaveCityData = {};
Page({
    data: {
        citycode: "sz",
        city: '深圳',
        province: '广东',
        pindex: 0,
        cindex: 0,
        cityPick: ["选择城市"],
        showNav: true, // 快捷导航是否展开
        showMore: false,
        showShare: true, // 显示分享按钮
        leaveProvPick: [],
        leaveCityPick: [],
        leavePindex: 0,
        leaveCindex: 0,
        showMore: true,
    },

    onLoad: function(options) {
        util.getUrl(app.globalData.url).then(res => {
            this.getProvinceData(options.city);
            const query = wx.createSelectorQuery()
            setTimeout(() => {
                query.select('.change-city').boundingClientRect()
                query.selectViewport().scrollOffset()
                query.exec(res => {
                    if (res && Array.isArray(res))
                        this.setData({
                            marginTop: res[0].height
                        })
                })
            }, 200);

        });
    },

    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                let province = res.result.address_component.province;
                let provinceIndex, cityIndex, toProvinceIndex;
                // let city="芜湖市";
                let citydata;
                if (province && city) {
                    let findProvince = this.data.leaveProvPick.filter(pro => province.includes(pro.province))[0];
                    provinceIndex = this.data.leaveProvPick.findIndex(pro => province.includes(pro.province));
                    toProvinceIndex = this.data.provPick.findIndex(pro => province.includes(pro.province));
                    if (findProvince) {
                        citydata = findProvince.citylist.filter(findCity => city.includes(findCity.cityname))[0];
                        cityIndex = findProvince.citylist.findIndex(findCity => city.includes(findCity.cityname));
                    }
                }

                if (citydata != '' && city.indexOf(this.data.city) == -1) {
                    wx.showModal({
                        content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '隔离政策查询？',
                        success: res => {
                            if (res.confirm) {
                                this.setData({
                                    city: citydata.cityname,
                                    citycode: citydata.citycode,
                                    province,
                                    leavePindex: provinceIndex,
                                    leaveCindex: cityIndex,
                                    pindex: toProvinceIndex,
                                    cindex: 0,
                                });
                                // let clist = [];
                                // this.data.provPick.forEach((v, k) => {
                                //     if (v == this.data.province) {
                                //         this.setData({
                                //             pindex: k,
                                //         })
                                //     }
                                // })
                                // this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                //     clist.push(v.cityname)
                                // })
                                // this.setData({
                                //     cityPick: this.data.cityPick.concat(clist)
                                // })
                                // this.data.cityPick.forEach((v, k) => {
                                //     if (v == this.data.city) {
                                //         this.setData({
                                //             cindex: k,
                                //         })
                                //     }
                                // })
                                this.getQrcode();
                                this.getInfoData();
                            }
                        }
                    })
                }

            },
        });
    },

    getInfoData() {
        util.request('zhuanti.php', {
            action: "hesuan_all",
            type: "city",
            citycode: this.data.citycode
        }, ).then(res => {
            if (res.data && res.data.data.data) {
                res.data.data.data.forEach(item => item.over = false);
                this.info = res.data.data;
                if (res.data.data.hasOwnProperty("daofen") && res.data.data.daofen) {
                    res.data.data.daofen.content = res.data.data.daofen.content
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                        .replace(/<p>/ig, '<p class="p_class">')
                        .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                        .replace(/<img/gi, '<img style="width:100%;height:auto"')
                        // .replace(/<span>/ig, '<p class="p_class">')
                }
                this.setData({
                    infoData: res.data.data.data,
                    daofen: res.data.data.daofen,
                });
                this.title = res.data.data.title;
                let data;
                if (leaveCityData.citycode)
                    data = this.data.infoData.filter(item => item.leavecity == this.data.city && item.cityname == leaveCityData.cityname);
                else
                    data = this.data.infoData.filter(item => item.leavecity == this.data.city).slice(0, 5);
                if (this.data.infoData.length > 5 && data.length >= 5) {
                    this.setData({
                        showMore: true,
                    });
                }

                allInfo = this.data.infoData.filter(item => item.leavecity == this.data.city);
                this.setData({
                    geliInfo: data,
                });
            } else {
                wx.showToast({
                    title: '请求数据失败，请稍后重试',
                    icon: 'none',
                });
            }
        });

    },

    showMoreInfo() {
        this.setData({
            geliInfo: allInfo,
            showMore: false,
        })
    },

    getProvinceData(optcode) {
        util.getUrl(app.globalData.url).then(() => {
            let data = {
                action: "hesuan_all",
                type: "city",
                citycode: optcode ? optcode : this.data.citycode
            };
            util.request('zhuanti.php', data, ).then(res => {
                if (res.data && res.data.data.data) {
                    if (res.data.data.cityname)
                        res.data.data.cityname.city.forEach(citys => {
                            citys.citylist.unshift({ cityname: "选择城市" })
                        });
                    if (res.data.data.leave)
                        res.data.data.leave.city.forEach(citys => {
                            citys.citylist.unshift({ cityname: "选择城市" });
                        });
                    this.setData({
                        leaveProvPick: res.data.data.leave.city,
                        provPick: res.data.data.cityname.city,
                        infoData: res.data.data.data,
                    });
                    this.info = res.data.data;
                    let leavePindex = this.data.leaveProvPick.findIndex(item => item.province == this.data.province) ? this.data.leaveProvPick.findIndex(item => item.province == this.data.province) : 0;
                    let leaveCindex = this.data.leaveProvPick[leavePindex].citylist.findIndex(item => item.cityname == this.data.city);
                    let toPindex = this.data.provPick.findIndex(item => item.province == this.data.province) ? this.data.provPick.findIndex(item => item.province == this.data.province) : 0;
                    this.setData({
                        leavePindex,
                        leaveCindex,
                        pindex: toPindex,
                        cindex: 0,
                    });

                    if (res.data.data.leave.city && optcode)
                        res.data.data.leave.city.forEach((provinces) => provinces.citylist.forEach(city => {
                            if (city.citycode == optcode)
                                this.setData({
                                    leavePindex: this.data.leaveProvPick.findIndex(item => item.province == provinces.province),
                                    leaveCindex: this.data.leaveProvPick[this.data.leaveProvPick.findIndex(item => item.province == provinces.province)].citylist.findIndex(item => item.citycode == optcode),
                                    city: city.cityname,
                                    citycode: city.citycode,
                                });
                        }));

                    let data = this.data.infoData.filter(item => item.leavecity == this.data.city).slice(0, 5);
                    data.forEach(item => item.over = false);
                    if (this.data.infoData.length > 5) {
                        allInfo = this.data.infoData.filter(item => item.leavecity == this.data.city);
                        this.setData({
                            showMore: true,
                        });
                    }
                    if (res.data.data.hasOwnProperty("daofen") && res.data.data.daofen) {
                        res.data.data.daofen.content = res.data.data.daofen.content
                            .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                            .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                            .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                            .replace(/<p>/ig, '<p class="p_class">')
                            .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                            .replace(/<img/gi, '<img style="width:100%;height:auto"')
                            // .replace(/<span>/ig, '<p class="p_class">')
                    }
                    this.setData({
                        geliInfo: data,
                        daofen: res.data.data.daofen,
                    });
                    if (!optcode)
                        this.getLocation();
                    this.getQrcode();
                } else if (res.data) {
                    wx.showToast({
                        title: '当前城市暂无隔离信息',
                        icon: 'none',
                        success: res => this.getProvinceData('sz')
                    });
                }
            });
        })
    },

    bindProv(e) {
        this.setData({
            leavePindex: e.detail.value,
            leaveCindex: 0,
        });
    },

    bindToProv(e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
        });
    },

    bindCity(e) {
        let cityData = this.data.leaveProvPick[this.data.leavePindex].citylist[e.detail.value];
        this.setData({
            leaveCindex: e.detail.value,
            citycode: cityData.citycode,
            city: cityData.cityname
        });
        this.getInfoData();
    },

    bindToCity(e) {
        leaveCityData = this.data.provPick[this.data.pindex].citylist[e.detail.value];
        if (leaveCityData.cityname == this.data.city && leaveCityData.citycode == this.data.citycode) {
            wx.showToast({
                title: '出发城市与到达城市不能相同',
                icon: 'none',
            });
            return;
        }
        if (leaveCityData.cityname)
            this.setData({
                cindex: e.detail.value,
            });
        if (leaveCityData.cityname == "选择城市") {
            this.setData({
                geliInfo: allInfo.filter(item => item.leavecity == this.data.city).slice(0, 5),
                showMore: true,
            });
            return;
        }

        let geliInfo = allInfo.filter(item => item.cityname == leaveCityData.cityname && item.leavecity == this.data.city);
        this.setData({
            geliInfo,
            showMore: false,
        });

    },

    // 获取城市公众号二维码
    getQrcode() {
        util.cityConfig(this.data.citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode,
            })
        })
    },

    detail(e) {
        let index = e.currentTarget.dataset.index;
        this.data.geliInfo[index].over = !this.data.geliInfo[index].over;
        this.setData({
            geliInfo: this.data.geliInfo,
        });
    },

    openDetail(e) {
        let data = this.data.geliInfo[e.currentTarget.dataset.index];
        if (data.arr)
            wx.navigateTo({
                url: `/pages/article/detail/detail?id=${data.arr.id}&type=${data.arr.type}&city=${data.arr.citycode ? data.arr.citycode : this.data.citycode}`,
            });
    },

    onPageScroll: function(e) {
        this.setData({
            showNav: false,
        });
    },

    onShareAppMessage: function() {
        return {
            title: this.info.title ? this.info.title : "隔离政策查询",
        }
    },
})